
package com.modele.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.main.EasyLunch;
import com.modele.dao.DAO;
import com.modele.entity.Carte;
import com.modele.entity.Consomptible;
import com.modele.entity.Menu;


public class CarteDAO extends DAO<Carte>{

	public CarteDAO(Connection connexion) {
		super(connexion);
	}

	@Override
	public boolean create(Carte obj) {
		try {
			boolean res;
			int i = 0;
			for (i = 0 ; i< obj.getListMenu().size() ; i++) {
			PreparedStatement statement = connexion.prepareStatement("INSERT INTO carte " +
				"(listMenu) " +
				"VALUES (?,?)");
			statement.setString(1, obj.getListMenu().get(i).getNomMenu());
			statement.setFloat(2, obj.getListMenu().get(i).getPrix());
			statement.executeUpdate();
			}
			for (i = 0 ; i< obj.getListConsomptible().size() ; i++) {
				PreparedStatement statement = connexion.prepareStatement("INSERT INTO carte " +
					"(listConsomptible) " +
					"VALUES (?)");
				statement.setString(1, obj.getListConsomptible().get(i).getNom());
				statement.setFloat(2, obj.getListConsomptible().get(i).getPrix());
				statement.executeUpdate();
				}
			res = true;
			return res;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
	}

	@Override
	public boolean delete(Carte obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(Carte obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Carte find(int id) 
	{		
		Carte carte = new Carte();
		carte.setIdCarte(id);
		
		List <Consomptible> listConsomptible = new ArrayList<Consomptible>();
		List <Menu> listMenu = new ArrayList<Menu>();
		
		carte.setListConsomptible(listConsomptible);
		carte.setListMenu(listMenu);
		
		MenuDAO menuDAO = (MenuDAO)EasyLunch.getFactory().getMenuDAO();
		ConsomptibleDAO consomptibleDAO = (ConsomptibleDAO)EasyLunch.getFactory().getConsomptibleDAO();
		
		try 
		{
			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM carte WHERE idCarte = ?");
			statement.setInt(1, id);
			
			if(statement.execute())
			{
				ResultSet result = statement.getResultSet();
				while(result.next())
				{
					Integer idMenu = result.getInt(2);
					Integer idConsomptible = result.getInt(3);
					
					if(idMenu != 0)
					{
						listMenu.add(menuDAO.find(idMenu));
					}
					else if(idConsomptible != 0)
					{
						listConsomptible.add(consomptibleDAO.find(idConsomptible));
					}
				}
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return carte;
	}
	
	@Override
	public List<Carte> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Carte findByValue(Object obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isPresent(Object obj) {
		// TODO Auto-generated method stub
		return false;
	}

	}


